/******************************************************************************
 * Copyright (C) 2015  671643387@qq.com
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 *****************************************************************************/

#ifndef SHARED_UTIL_LOCKEDQUEUE_H_
#define SHARED_UTIL_LOCKEDQUEUE_H_

#if defined(_MSC_VER) && (_MSC_VER >= 1200)
# pragma once
#endif

#include <deque>
#include "Shared/Global_Macros.h"
#include "Shared/Base/LockGuard.h"


namespace Shared {
namespace Util {

    template< typename T, typename LockType, typename StorageType = std::deque<T> >
    class LockedQueue
    {
    public:
        LockedQueue(void)
            : _lock()
            , _queue()
            , _canceled(false)
        {
        }
        virtual ~LockedQueue(void)
        {
        }


        void add(const T& item)
        {
            SHARED_LOCK_GURAD_ASSERT(_lock);
            _queue.push_back(item);
        }

        bool next(T& result)
        {
            SHARED_LOCK_GURAD_ASSERT_RETURN_VAR(_lock, false);

            if (_queue.empty())
            { return false; }

            result = _queue.front();
            _queue.pop_front();

            return true;
        }

        template<class Checker>
        bool next(T& result, Checker& check)
        {
            SHARED_LOCK_GURAD_ASSERT_RETURN_VAR(_lock, false);

            if (_queue.empty())
            { return false; }

            result = _queue.front();
            if (!check.process(result))
            { return false; }

            _queue.pop_front();
            return true;
        }

        T& peek(void)
        {
            lock();
            T& result = _queue.front();
            return result;
        }

        void cancel(void)
        {
            SHARED_LOCK_GURAD_ASSERT(_lock);
            _canceled = true;
        }

        bool cancelled(void)
        {
            SHARED_LOCK_GURAD_ASSERT(_lock);
            return _canceled;
        }

        void lock(void)
        {
            this->_lock.lock();
        }

        void unlock(void)
        {
            this->_lock.unlock();
        }

        bool empty(void)
        {
            SHARED_LOCK_GURAD_ASSERT(_lock);
            return _queue.empty();
        }

    private:
        LockType _lock;
        StorageType _queue;
        volatile bool _canceled;
    };

}}

#endif // SHARED_UTIL_LOCKEDQUEUE_H_
